﻿using UnityEngine;
using UnityEditor;
using System.Collections;

public class ObjectWindow : EditorWindow {

	static ObjectWindow currentWindow;

	GameObject userDefinedObject;

	int selectCount = 0;

	//Hierarchy中被选择的项目
	GameObject[] selectObjects = new GameObject[0];

	[MenuItem("GameMenu/Object Window")]
	static void Init(){
		currentWindow = (ObjectWindow)EditorWindow.GetWindow(typeof(ObjectWindow));

		currentWindow.title = "Editor Defined";
		currentWindow.minSize = new Vector2 (512, 256);
	}

	void OnGUI(){

		selectObjects = Selection.gameObjects;
		if (selectObjects.Length > 0) {
//			Debug.Log ("==> " + selectObjects.Length.ToString() + "objects selected");
			selectCount = selectObjects.Length;
		} else {
			selectCount = 0;
//			Debug.Log ("==> nothing selected!");
		}





		GUILayout.BeginHorizontal ();
		GUILayout.Space (10);

		GUILayout.BeginVertical ();
		GUILayout.Space (10);

		GUILayout.Label ("Editor Window Tool.", EditorStyles.boldLabel);

		GUILayout.BeginHorizontal ();
		GUILayout.Space (10);
		userDefinedObject = (GameObject)EditorGUILayout.ObjectField(
			"User Defined Object:", userDefinedObject, typeof(GameObject), true
			) as GameObject;

		GUILayout.Space (10);
		GUILayout.EndHorizontal ();

		GUILayout.BeginHorizontal ();
		GUILayout.Space (10);
		EditorGUILayout.LabelField ("Selected Objects Count: " + selectCount.ToString());
		GUILayout.Space (10);
		GUILayout.EndHorizontal ();


		if (selectObjects.Length > 0 && userDefinedObject) {
			if(GUILayout.Button("Replace Objects", GUILayout.Height(40f)))
			{
//				ReplaceObjects();
				EditorUtility.ReplaceObjects(userDefinedObject, selectObjects);
			}
		}


		GUILayout.Space (10);
		GUILayout.EndVertical ();
		GUILayout.Space (10);
		GUILayout.EndHorizontal ();

		Repaint ();
	}





}
